<?php

require($_SERVER['DOCUMENT_ROOT'] . "/bootstrap.php");

// lay thong tin tim kiem: loai, gia
if (isset($_POST['submit_search'])) { // Nếu có nhấn nút submit
	switch ($_POST['search_price']) {
		case 0:
			$price_from = 0;
			$price_to = 1000000000;
			break;
		case 1:
			$price_from = 0;
			$price_to = 1000000;
			break;
		case 2:
			$price_from = 1000000;
			$price_to = 3000000;
			break;
		case 3:
			$price_from = 3000000;
			$price_to = 5000000;
			break;
		case 4:
			$price_from = 5000000;
			$price_to = 8000000;
			break;
		case 5:
			$price_from = 8000000;
			$price_to = 12000000;
			break;
		case 6:
			$price_from = 12000000;
			$price_to = 20000000;
			break;
		case 7:
			$price_from = 20000000;
			$price_to = 1000000000;
			break;
		default :
			$price_from = 0;
			$price_to = 1000000000;
			break;
	}

	if ($_POST['search_price'] != 0) {
		$_SESSION['search_price'] = $_POST['search_price'];
		$_SESSION['search_price_from'] = $price_from;
		$_SESSION['search_price_to'] = $price_to;
	} else {
		unset($_SESSION['search_price']);
		unset($_SESSION['search_price_from']);
		unset($_SESSION['search_price_to']);
	}

	if ($_POST['search_category'] != 0)
		$_SESSION['search_category'] = $_POST['search_category'];
	else
		unset($_SESSION['search_category']);
	
	if($_POST['search_productname'] != '')
	{
		$_SESSION['search_productname'] = $_POST['search_productname'];
	}
	else {
		unset($_SESSION['search_productname']);
	}
}
if (isset($_SESSION['search_price_from']) && isset($_SESSION['search_price_to']))
	$condition_price = " and p.price > {$_SESSION['search_price_from']} and p.price < {$_SESSION['search_price_to']} ";
else
	$condition_price = " ";

if (isset($_SESSION['search_category']))
	$condition_category = " and c.idcategory = {$_SESSION['search_category']} ";
else
	$condition_category = " ";

if(isset($_SESSION['search_productname']))
	$condition_productname = " and p.productname like '%{$_SESSION['search_productname']}%'";
else
	$condition_productname = " ";
//end

$display = 9;
$current=0;
$next=0;
$prev=0;
//tính tổng số trang cần hiện thị
if (isset($_GET['page']) && is_int($_GET['page'])) {
	$page = $_GET['page'];
} else {//nếu chưa xác định thì tìm số trang 
	$query = mysql_query(@"select count(idproduct) from product p,category c where p.idcategory=c.idcategory" . $condition_category . $condition_price . $condition_productname);
	$row = mysql_fetch_array($query);
	$record = $row[0];
	if ($record > $display) {
		$page = ceil($record / $display); //làm tròn lên
	} else {
		$page = 1;
	}
}
$start = isset($_GET['start']) && (int) $_GET['start'] ? $_GET['start'] : 0;

$sql2 = mysql_query("select * from product p,category c where p.idcategory=c.idcategory" . $condition_category . $condition_price . $condition_productname . "order by productname limit $start,$display");

if ($page > 1) {
	$next = $start + $display; //9
	$prev = $start - $display; // -9
	$current = ($start / $display) + 1;
}
$product='';
while ($set = mysql_fetch_array($sql2)) {
	$product[] = $set;
}
$smarty->assign('record', $record);
$smarty->assign('current', $current);
$smarty->assign('display', $display);
$smarty->assign('page', $page);
$smarty->assign('prev', $prev);
$smarty->assign('next', $next);
$smarty->assign('product', $product);
$smarty->display('../templates/search/index.tpl');
?>


